package com.peanutplan.blog.controller.admin;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.peanutplan.blog.controller.BaseController;
import com.peanutplan.blog.dao.SensitiveWordMapper;
import com.peanutplan.blog.exception.TipException;
import com.peanutplan.blog.model.bo.RestResponseBo;
import com.peanutplan.blog.model.vo.SensitiveWord;
import com.peanutplan.blog.model.vo.SensitiveWordExample;
import com.peanutplan.blog.service.ISensitiveWordsService;

/**
 * Created by 13 on 2017/2/26.
 */
@Controller
@RequestMapping("admin/sensitiveWords")
public class SensitiveWordsController extends BaseController {

    private static final Logger LOGGER = LoggerFactory.getLogger(SensitiveWordsController.class);

    @Resource
    private ISensitiveWordsService sensitiveWordsService;
    @Resource
    private SensitiveWordMapper sensitiveWordMapper;

    @PostMapping(value = "save")
    @ResponseBody
    @Transactional(rollbackFor = TipException.class)
    public RestResponseBo save(@RequestParam String sensitiveWord, @RequestParam String replaceWord) {
        try {
        	
        	SensitiveWordExample example = new SensitiveWordExample();
        	example.createCriteria().andSensitiveWordEqualTo(sensitiveWord);
        	long count = sensitiveWordMapper.countByExample(example);
        	if(count>0) {
        		return RestResponseBo.fail("已经存在敏感词【"+sensitiveWord+"】，请不要重复添加！");
        	} 
        	
        	SensitiveWord sw = new SensitiveWord();
            sw.setSensitiveWord(sensitiveWord);
            sw.setReplaceWord(replaceWord);
            sw.setStatus(1);
            sensitiveWordsService.insertSensitiveWord(sw);
        } catch (Exception e) {
            String msg = "保存失败";
            if (e instanceof TipException) {
                msg = e.getMessage();
            } else {
                LOGGER.error(msg, e);
            }
            return RestResponseBo.fail(msg);
        }
        return RestResponseBo.ok();
    }
    
    /**
     * 评论列表
     * @param page
     * @param limit
     * @param request
     * @return
     */
    @GetMapping(value = "")
    public String index(@RequestParam(value = "page", defaultValue = "1") int page,
                        @RequestParam(value = "limit", defaultValue = "10") int limit, HttpServletRequest request) {
        PageInfo<SensitiveWord> p = sensitiveWordsService.getSensitiveWordsWithPage(null, page, limit);
        request.setAttribute("sensitiveWords", p);
        return "admin/sensitiveWords_list";
    }

    /**
     * 删除一条评论
     * @param coid
     * @return
     */
    @PostMapping(value = "delete")
    @ResponseBody
    @Transactional(rollbackFor = TipException.class)
    public RestResponseBo delete(@RequestParam Integer id) {
        try {
            sensitiveWordsService.delete(id);
        } catch (Exception e) {
            String msg = "删除失败";
            if (e instanceof TipException) {
                msg = e.getMessage();
            } else {
                LOGGER.error(msg, e);
            }
            return RestResponseBo.fail(msg);
        }
        return RestResponseBo.ok();
    }

    @PostMapping(value = "status")
    @ResponseBody
    @Transactional(rollbackFor = TipException.class)
    public RestResponseBo delete(@RequestParam Integer id, @RequestParam Integer status) {
        try {
        	SensitiveWord sensitiveWord = new SensitiveWord();
        	sensitiveWord.setId(id);
        	sensitiveWord.setStatus(status);
            sensitiveWordsService.update(sensitiveWord);
        } catch (Exception e) {
            String msg = "操作失败";
            if (e instanceof TipException) {
                msg = e.getMessage();
            } else {
                LOGGER.error(msg, e);
            }
            return RestResponseBo.fail(msg);
        }
        return RestResponseBo.ok();
    }


}
